<?php
	include_once("db/Department.inc");
	include_once("UserView.php");

	$d = null;
	
	if (isset($_REQUEST["code"]))
		$d = Department::getDepartment($_REQUEST["code"]);
	
	$isNew = $d == null;
	$title = ($isNew) ? "Add New Department" : "Edit Department - $d->id";
?>

<h1><?php print $title?></h1>
	
<form id="deptForm" method="post" action="?action=setDepartment">
	<input type="hidden" id="oldCode" name="oldCode" value="<?php print ($isNew) ? 0 : $d->id ?>" />
	<table cellspacing="0" class="formLayout">
		<?php if (isset($_REQUEST["error"]) && $_REQUEST["error"] == "deptCodeExists") { ?>
			<tr>
				<td colspan="2" class="error">
					The department code you have entered already exists.  Please
					use another one.
				</td>
			</tr>
		<?php } ?>
		<tr>
			<td>Code</td>
			<td><input type="text" id="code" name="code" maxlength="4"
					<?php print (!$isNew) ? "readonly" : "" ?> 
					value="<?php 
						if (!$isNew)
							print $d->id;
					?>" /></td>
		</tr>
		
		<tr>
			<td>Description</td>
			<td><input id="description" type="text" name="description" maxlength="40"
				 value ="<?php if (!$isNew) {
						print $d->description;
					 } ?>" />
			<td>
		</tr>
		
		<tr colspan="2">
			<td>
				<input type="submit" value="Confirm" class="button"/>
				<input type="button" value="Cancel" class="button" onClick="window.location.href='?action=viewDepartments'" />
				
				<!--
				<a href="./?action=viewDepartments" ><input type="button" class="button" value="Cancel" /></a>
				-->
			</td>
		</tr>
	</table> 
</form>
